import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'
Vue.use(Vuex)
Vue.config.productionTip = false
const store = new Vuex.Store({
  state: {
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false,
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false,
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false,
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: false,
      },
    ],
  },
  mutations: {
    //减少
    subCount(state, i) {
      if (state.goodList[i].num <= 1) {
        state.goodList[i].num = 1
      } else {
        state.goodList[i].num--
      }
    },
    //添加
    addCount(state, i) {
      state.goodList[i].num++
    },
    //删除
    delGood(state, name) {
      state.goodList = state.goodList.filter((item) => item.name !== name)
    },
  },
  getters: {
    //总价
    allPrice(state) {
      return state.goodList.reduce((sum, item) => {
        if (item.checked) {
          sum += item.price * item.num
        }
        return sum
      }, 0)
    },
    Price(state) { 
      return state.goodList.map(item => {
        return item.price * item.num
      })
    }
  },
})
new Vue({
  store,
  render: (h) => h(App),
}).$mount('#app')
